<?php

namespace App\Controller\Admin;

use App\Controller\Example\FundsExample;
use App\Controller\Example\UserExample;
use App\Models\AdminModel;
use App\Models\Conn;
use App\Models\FundsModel;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Enums\FundsGenreEnum;
use Core\Result\Result;
use Core\Utils\Utils;

class User extends UsersModel
{
    /**
     * 管理后台用户列表
     * entry token,id,truename,phone,pid,startTime,endTime,page,limit
     * return json
     * */
    public function userList($data){
        $assemble = Common::assemble(new UserExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE 1 ";
        if($assemble->id !=null){ $joinSql.=" and `id` ='$assemble->id' ";}
        if($assemble->truename !=null){ $joinSql.=" and `truename` ='$assemble->truename' ";}
        if($assemble->phone !=null){ $joinSql.=" and `phone` ='$assemble->phone' ";}
        if($assemble->pid !=null){ $joinSql.=" and `pid` ='$assemble->pid' ";}
        if($assemble->service !=null){ $joinSql.=" and `service` >='$assemble->service' ";}
        if($assemble->startTime !=null){ $assemble->startTime = strtotime($assemble->startTime); $joinSql.=" and UNIX_TIMESTAMP(`create_time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $assemble->endTime = strtotime($assemble->endTime); $joinSql.=" and UNIX_TIMESTAMP(`create_time`) <= '$assemble->endTime' ";}
        $userSql = "SELECT `id`,`uuid`, `username`, `truename`, `pid`,`sid`, `level`,`svip` ,`money`, `integral`,`tinymoney`, `id_card`, `login_ip`, `create_time`, `last_time`, `status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`, `peer_reward`, `agent_reward`, `partner_reward`,`total_reward`,`service` FROM `users` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($userSql,true);
        $returnData["list"]=$readConn;
        $userCountSql="SELECT count(id) as count FROM `users` $joinSql";
        $userTotalSql="SELECT sum(`money`) as 总通用余额, sum(`tinymoney`) as 总专属余额,sum(`total_reward`) as 总奖励,sum(`ach`) as 总业绩 FROM `users` $joinSql limit $identical->pageStart,$identical->limit";
        $returnData["count"] = $conn->readConn($userCountSql)["count"];
        $returnData["total"] = $conn->readConn($userTotalSql);
        Result::success($returnData);
    }
    /**
     * 管理后台编辑用户状态
     * entry uid
     * return json
     * */
    /*public function editUserState($data){
        $assemble = Common::assemble(new U_KExample(), $data);
        $conn = new Conn();
        if($assemble->switch != 1){
            $assemble->switch_f = 0;
            $assemble->switch_z = 0;
            $assemble->switch_h = 0;
            $assemble->switch_g = 0;
            $assemble->switch_j = 0;
            $assemble->switch_gq = 0;
        }
        $conn->shiwuStart();
        if(self::editUserStateInside($conn,$assemble) != 1){
            $conn->shiwuRollback();
            Result::error("编辑失败");
        }
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        AdminLog::addAdminLog($adminInToken["username"],"用户管理","编辑用户开关:[".$assemble->uid."]");
        $conn->shiwuEnd();
        Result::success("操作成功");
    }*/
    /**
     * 管理后台用户充值
     * entry token,id,total,type
     * return json
     * */
    public function userRecharge($data){
        $total = $data["total"];
        $type = $data["type"];
        if($total == 0 || $total ==null || $total > 1000000){
            Result::error(ErrorEnum::QUANTITYERROR);
        }
        $assemble = Common::assemble(new UserExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        if(empty($adminInToken) || $adminInToken["level"] !=1){
            Result::error(ErrorEnum::NOPERMISSIONTOSUBMIT);
        }
        $userInsideId = UsersModel::getUserInsideId($conn, $assemble->id);
        if(empty($userInsideId)){
            Result::error(ErrorEnum::USERNAMENULL);
        }
        $uid = $userInsideId["id"];
        $money = $userInsideId["money"];
        $integral = $userInsideId["integral"];
        $tinymoney = $userInsideId["tinymoney"];
        $service = $userInsideId["service"];
        //money,integral
        $fundsExample = new FundsExample();
        $userJoin="";
        switch ($type){
            case 1:
                $userJoin = " `money` = `money`+ $total ";
                $fundsExample->genre = FundsGenreEnum::MONEY;
                $fundsExample->balance = $userInsideId["money"] + $total;
                $mo = "通用余额";
                break;
            case 2:
                $userJoin = " `tinymoney` = `tinymoney`+ $total ";
                $fundsExample->genre = FundsGenreEnum::TINYMONEY;
                $fundsExample->balance = $userInsideId["tinymoney"] + $total;
                $mo = "专属余额";
                break;
            default:
                Result::error(ErrorEnum::STATUSERROR);
                break;
        }
        $fundsExample->uid = $uid;
        $fundsExample->amount = $total;
        $fundsExample->notes = "后台操作-".$adminInToken["id"];
        $fundsExample->number = Utils::createOrderNo();
        if($total > 0){
            $fundsExample->type = "后台充值";
        }else{
            $fundsExample->type = "后台扣除";
        }
        $conn->shiwuStart();
        $upUser = "UPDATE `users` set $userJoin WHERE `id` = $uid and `money` = $money and `tinymoney` = $tinymoney and `service` = $service";
        if(FundsModel::insertFunds($conn,$fundsExample) !=1 || $conn->executeConn($upUser) !=1 || empty($userJoin)){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::MODIFICATIONFAILED);
        }
        AdminLog::addAdminLog($adminInToken["username"],"用户管理","用户充值:id:".$uid."[".$total."]".$mo);
        $conn->shiwuEnd();
        Result::success('ok');
    }
    /**
     * 管理后台编辑用户
     * entry token,id,status,agent
     * return json
     * */
    public function editUsers($data){
        $assemble = Common::assemble(new UserExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $userInsideId = UsersModel::getUserInsideId($conn, $assemble->id);
        $assemble->token = $userInsideId["token"];
        $conn->shiwuStart();
        if(UsersModel::upUserInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"用户管理","编辑用户级别:id:".$assemble->id."[".$assemble->level."]");
        Result::success("ok");
    }
}